package com.eclipsemail.core.message;

import javax.mail.internet.InternetAddress;

import com.google.common.base.Preconditions;

public class NetAddress {

    private String name;
    private String mail;

    public NetAddress(String name, String mail) {
        this.name = name;
        this.mail = mail;
    }

    public NetAddress(InternetAddress internetAddress) {
        Preconditions.checkNotNull(internetAddress);

        this.name = internetAddress.getPersonal();
        this.mail = internetAddress.getAddress();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Address [mail=");
        builder.append(mail);
        builder.append(", name=");
        builder.append(name);
        builder.append("]");
        return builder.toString();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((mail == null) ? 0 : mail.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        NetAddress other = (NetAddress) obj;
        if (mail == null) {
            if (other.mail != null) {
                return false;
            }
        } else if (!mail.equals(other.mail)) {
            return false;
        }
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }

}
